package com.showcai.controller.watchEnroll;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.ApplyWatch;
import com.showcai.module.ExpressInfo;
import com.showcai.module.Prizes;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.ApplyService;
import com.showcai.service.ExpressInfoService;
import com.showcai.service.GetUserInfoService;
import com.showcai.service.OrderService;
import com.showcai.util.ExcelIn;
import com.showcai.util.ExcelOut;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Baggio on 2018-9-6.
 */
@RequestMapping("/watchEnroll")
@Controller
public class WatchEnrollController extends BaseController {

    @Autowired
    ApplyService applyWatchService;
    @Autowired
    ExpressInfoService expressInfoService;
    @Autowired
    OrderService orderService;
@Autowired
    GetUserInfoService getUserInfoService;

    /**
     * 查询所有报名用户
     *
     * */
    @RequestMapping(value = "/getWatchEnrollList")
    @ResponseBody
    public ModelAndView getWatchEnrollList() {
        ModelAndView mov = new ModelAndView();
        Map<String,String> map=super.showRequestParameters();
        String startDate=map.get("startDate");
        String offlineDate=map.get("offlineDate");
        if(StringUtils.isNotBlank(startDate)){
            String startDate1=startDate+" 00:00:00";
            map.put("startDate",startDate1);
        } if(StringUtils.isNotBlank(offlineDate)){
            String offlineDate1=offlineDate+" 23:59:59";
            map.put("offlineDate",offlineDate1);
        }
        PageInfo<ApplyWatch> list =  applyWatchService.selectApplyPageList(map);
        map.put("startDate",startDate);
        map.put("offlineDate",offlineDate);
        mov.addObject("pageinfo",list);
        mov.addObject("bean",map);
        mov.setViewName("/watchEnroll/watchEnrollList");
        return mov;
    }
    /**
     * 查询所有报名用户
     *
     * */
    @RequestMapping(value = "/applyWatchList")
    @ResponseBody
    public ModelAndView applyWatchList() {
        ModelAndView mov = new ModelAndView();

        Map<String,String> map=super.showRequestParameters();
        PageInfo<ApplyWatch> list =  applyWatchService.selectApplyPageList(map);
        mov.addObject("pageinfo",list);
        mov.addObject("bean",map);
        mov.setViewName("/watchEnroll/applyWatchList");
        return mov;
    }
    /**
     * 付费899的用户
     *
     * */
    @RequestMapping(value = "/waitPayChargeList")
    @ResponseBody
    public ModelAndView waitPayChargeList(Integer page,Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        Map<String,String> map=super.showRequestParameters();
        map.put("page",String.valueOf(page));
        map.put("pageSize",String.valueOf(pageSize));
        map.put("status",String.valueOf(OrderService.RECEIVED));
        map.put("orderPrice",String.valueOf(89900));
        PageInfo<ApplyWatch> list =  applyWatchService.selectApplyPageList(map);
        mov.addObject("pageInfo",list);
        mov.addObject("bean",map);
        mov.setViewName("/watchEnroll/waitPayChargeList");
        return mov;
    }
    /**
     * 导出Excel
     * @return
     */
    @RequestMapping(value = "/waitPayChargeOut")
    @ResponseBody
    public void waitPayChargeOut() {
        Map<String,String> map=super.showRequestParameters();
        String[] strList = {"姓名","电话","订单状态","创建时间"};
        map.put("status",String.valueOf(OrderService.RECEIVED));
        map.put("orderPrice",String.valueOf(90900));
        List<ApplyWatch> eventRegistrationList = applyWatchService.selectApplyList(map);
        String fileName = "waitPayCharge"+ new SimpleDateFormat("yyyyMMdd").format(new Date());
        List<LinkedList<String>> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (ApplyWatch applyWatch : eventRegistrationList) {
            LinkedList<String> lmap = new LinkedList<>();
            lmap.add(applyWatch.getName());
            lmap.add(applyWatch.getWatchPhone());
            Byte status=applyWatch.getStatus();
            lmap.add(status==OrderService.DISPATCHED?"待收货":status==OrderService.RECEIVED?"已收货":"");

            if(applyWatch.getCreateTime()!=null){
                lmap.add(sdf.format(applyWatch.getCreateTime()));
            }else{
                lmap.add("");
            }
            list.add(lmap);
        }
        ExcelOut.excelOut(fileName,response,strList,list);
    }




    @RequestMapping(value = "/waitDispatchList")
    @ResponseBody
    public ModelAndView getWaitDispatchList() {
        ModelAndView mov = new ModelAndView();
        Map<String,String> map=super.showRequestParameters();
        map.put("status", String.valueOf(OrderService.WAIT_DISPATCH));
        PageInfo<ApplyWatch> list =  applyWatchService.selectApplyPageList(map);
        mov.addObject("pageinfo",list);
        mov.setViewName("/watchEnroll/waitDispatchList");
        return mov;
    }
    @RequestMapping(value = "/dispatchedList")
    public ModelAndView dispatchedList() {
        ModelAndView mov = new ModelAndView();
        Map<String,String> map=super.showRequestParameters();
        map.put("status", String.valueOf(OrderService.DISPATCHED));
        PageInfo<ApplyWatch> list =  applyWatchService.selectApplyPageList(map);
        mov.addObject("pageInfo",list);
        mov.setViewName("/watchEnroll/dispatchedList");
        return mov;
    }


    @RequestMapping(value = "/expressAdd")
    public ModelAndView expressAdd(Long orderId) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/watchEnroll/expressInfo");
        mov.addObject("orderId",orderId);
        return mov;
    }
    @RequestMapping(value = "/modifyApplyStatus")
    @ResponseBody
    public String modifyApplyStatus(ApplyWatch applyWatch) {
        ResultObj resultObj = new ResultObj();
        applyWatchService.modifyApply(applyWatch);
        orderService.modifyOrderStatus(applyWatch.getOrderId(),applyWatch.getStatus());
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }

    @RequestMapping(value = "/expressInfoAdd")
    @ResponseBody
    public String expressInfoAdd(ExpressInfo expressInfo) {
        ResultObj resultObj = new ResultObj();
        expressInfo.setCreateTime(new Date());
        expressInfoService.modifyExpressInfo(expressInfo);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
    /**
     * 导出Excel
     * @return
     */
    @RequestMapping(value = "/excelOut")
    @ResponseBody
    public void excelOut() {
        Map<String,String> map=super.showRequestParameters();
        String startDate=map.get("startDate");
        String offlineDate=map.get("offlineDate");
        if(StringUtils.isNotBlank(startDate)){
            String startDate1=startDate+" 00:00:00";
            map.put("startDate",startDate1);
        } if(StringUtils.isNotBlank(offlineDate)){
            String offlineDate1=offlineDate+" 23:59:59";
            map.put("offlineDate",offlineDate1);
        }
        System.out.println("拿到的开始时间————————"+startDate);
        System.out.println("拿到的结束时间————————"+offlineDate);
        String[] strList = {"姓名","电话","订单ID","批发商编码","商城ID","销售订单号","地址","订单状态","创建时间","快递单号","快递备注"};
        List<ApplyWatch> EventRegistrationList = applyWatchService.selectApplyList(map);
        String fileName = "watchEnroll"+ new SimpleDateFormat("yyyyMMdd").format(new Date());
        List<LinkedList<String>> list = new ArrayList<>();
        for (ApplyWatch applyWatch : EventRegistrationList) {
            LinkedList<String> lmap = new LinkedList<>();
            lmap.add(applyWatch.getName());
            lmap.add( applyWatch.getPhone());
            lmap.add(String.valueOf(applyWatch.getOrderId()));
            lmap.add( applyWatch.getDistributorCode());
            lmap.add( applyWatch.getDistributorName());
            lmap.add( applyWatch.getShopCenterId());
            lmap.add(applyWatch.getSaleOrderNo());
            lmap.add(applyWatch.getAddress());
            if(applyWatch.getStatus()==OrderService.UNPAY){
                lmap.add("未付款");
            }else if(applyWatch.getStatus()==OrderService.PAYED){
                lmap.add("已付款");
            }else if(applyWatch.getStatus()==OrderService.WAIT_DISPATCH){
                lmap.add("待发货");
            }else{
                lmap.add("");
            }
            if(applyWatch.getCreateTime()!=null){
                lmap.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(applyWatch.getCreateTime()));
            }else{
                lmap.add("");
            }
            list.add(lmap);
        }
        ExcelOut.excelOut(fileName,response,strList,list);
    }
    /**
     * 导入Excel
     * @return
     */
    @RequestMapping(value = "/excelIn")
    @ResponseBody
    public String excelIn(@RequestParam(value = "file", required = false)MultipartFile file) {
        ResultObj resultObj = new ResultObj();
        String[] strList = {"销售单号","服务单号","时间","业务号码","商城订单状态"};
        List<HashMap<Integer, String>> list = ExcelIn.excelIn(strList,file);
        getUserInfoService.syncOrderStatus(list);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
    /**
     * 导入地推销售Excel
     * @return
     */
    @RequestMapping(value = "/importPush")
    @ResponseBody
    public String importPush(@RequestParam(value = "importPushFile", required = false)MultipartFile importPushFile) {
        ResultObj resultObj = new ResultObj();
        String[] strList = {"名字","电话","订单号","所选号码","状态"};
        List<HashMap<Integer, String>> list = ExcelIn.excelIn(strList,importPushFile);
        getUserInfoService.syncOrderStatus(list);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }

    /**
     * 导入发货Excel
     * @return
     */
    @RequestMapping(value = "/waitDispatchExcelIn")
    @ResponseBody
    public String waitDispatchExcelIn(@RequestParam(value = "file", required = false)MultipartFile file) {
        ResultObj resultObj = new ResultObj();
        String[] strList = {"姓名","电话","订单ID","批发商编码","商城ID","销售订单号","地址","订单状态","创建时间","快递单号","快递备注"};
        List<HashMap<Integer, String>> list = ExcelIn.excelIn(strList,file);
        getUserInfoService.syncDispatchedOrderStatus(list);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
}
